
include std/unittest.e
include qt/qtSql.e
include qt/core.e as qt
include std/filesys.e



-- TEST_QUIET (so no output), TEST_SHOW_FAILED_ONLY and TEST_SHOW_ALL
--~ set_test_verbosity(TEST_SHOW_ALL)



--this currently works for QSQLITE, 
--depending on who compiled Qt and how it may not.
--and still missing many many necessary things

global object _ = 0
_ = qt:init()

--wouldn't compile w/mysql4 although it says it will
test_equal("QMYSQL available", 1, db_available("QMYSQL"))
test_equal("last error", "", db_lastError("QMYSQL"))
--no, didn't have sqlite sdk installed
test_equal("QSQLITE2 available", 1, db_available("QSQLITE2"))
--yes
test_equal("QODBC available", 1, db_available("QODBC"))
--yes
test_equal("QSQLITE available", 1, db_available("QSQLITE"))


    --in memory database
    if not db_addDatabase("QSQLITE", ":memory:") then
        --not sure why am getting the message twice
        test_equal("last error ", "Driver not loaded?", db_lastError())
    else
    atom query = db_Query()
        --from the Qt examples
    test_equal("create  ", 1, db_exec(query, `
___________create table person (id int primary key, 
               firstname varchar(20), lastname varchar(20))`
            ))

    test_equal("insert 1 ", 1, db_exec(query, `insert into person values(101, 'Danny', 'Young')`))
    test_equal("insert  2", 1, db_exec(query, `insert into person values(102, 'Christine', 'Holand')`))
    test_equal("insert  3", 1, db_exec(query, `insert into person values(103, 'Lars', 'Gordon')`))
        
        --~ db_setForwardOnly(query)
    test_equal("SELECT  1", 1, db_exec(query, `SELECT firstname, lastname FROM person;`))
    test_equal("isSelect   ", 1, db_isSelect(query))
    test_equal("isActive   ", 1, db_isActive(query))
        db_first(query)
    test_equal("isValid   ", 1, db_isValid(query))
    test_equal("get value 0  ", "Young", db_value(query, 1))
        --base 0
    test_equal("seek 3  ", 0, db_seek(query, 3))
        db_seek(query, 2)
    test_equal("isNull 3  ", 1, db_isNull(query, 3))
    test_equal("disk/isForwardOnly   ", 0, db_isForwardOnly(query))
    test_equal("get value 2  ", "Lars", db_value(query, 0))
        db_last(query)
    test_equal("disk/get value last2  ", "Lars", db_value(query, 0))
        
    --~ test_equal("disk/ previous 0  ", 0, db_previous(query))
        db_removeDatabase(":memory:")
    -- still isActive  &_isValid , seems like that could be a problem?
    end if

    if file_exists("disk.sdb") then
        --possibly test other things before deleting
        delete_file("disk.sdb")
    end if

    if not db_addDatabase("QSQLITE", "disk.sdb") then
        --not sure why am getting the message twice
        test_equal("disk/ last error ", "Driver not loaded?", db_lastError())
    else
    atom query = db_Query()
        --need to know the tables already exist? 
        --this shouldn't crash if disk.sdb exists, but does
        --not exactly sure where
    test_equal("disk/create  ", 1, db_exec(query, `
___________create table person (id int primary key, 
               firstname varchar(20), lastname varchar(20))`
            ))

    test_equal("disk/insert 1 ", 1, db_exec(query, `insert into person values(101, 'Danny', 'Young')`))
    test_equal("disk/insert  2", 1, db_exec(query, `insert into person values(102, 'Christine', 'Holand')`))
    test_equal("disk/insert  3", 1, db_exec(query, `insert into person values(103, 'Lars', 'Gordon')`))
    --think may need commit rollback etc, what about next run?
    test_equal("disk/isOpenError"  ,0 ,db_isOpenError("disk.sdb:"))
    test_equal("disk/isOpen " ,0 ,db_isOpen("disk.sdb:"))
        db_commit("disk.sdb:")
    test_equal("disk/SELECT  1", 1, db_exec(query, `SELECT firstname, lastname FROM person;`))
    test_equal("disk/isSelect   ", 1, db_isSelect(query))
    test_equal("disk/isActive   ", 1, db_isActive(query))
        db_first(query)
    test_equal("disk/isValid   ", 1, db_isValid(query))
    test_equal("disk/get value 0  ", "Young", db_value(query, 1))
        --base 0
    test_equal("disk/seek 3  ", 0, db_seek(query, 3))
        db_seek(query, 2)
    test_equal("disk/isNull 3  ", 1, db_isNull(query, 3))
    test_equal("disk/isForwardOnly   ", 0, db_isForwardOnly(query))
    test_equal("disk/get value 2  ", "Lars", db_value(query, 0))
        db_last(query)
    test_equal("disk/get value last2  ", "Lars", db_value(query, 0))
        db_previous(query)
    test_equal("disk/get value previous2  ", "Holand", db_value(query, 1))
        db_rollback("disk.sdb:")
        db_removeDatabase("disk.sdb:")
    -- still isActive  &_isValid , seems like that could be a problem?
    end if


test_report()

